<?php
global $options;

foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}

$dateformat = get_option('date_format');
$timeformat = get_option('time_format');

$currCat = get_the_category();
$parent = get_cat_name($currCat[0]->category_parent);

if ( is_category( 'individual-projects' ) ) {
	$cat_id = get_cat_ID( 'Projects' );
	wp_redirect( get_category_link( $cat_id ) ); exit;
}

//redirect to child if is archive for top-level parent
if ( is_category( $parent ) ) {
	first_subcat_redirect();
}

get_header(); ?>

<div id="content">
    <div class="wrap">
        <div class="column column-narrow">

<?php
echo "<p class='heading'>".$parent."</p>";
//list all categories, posts listed underneath, hide empty
list_cat_posts_all()
?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar: Archive Pages') ) : ?>&nbsp;<?php endif; ?>

        </div><!-- end .column-narrow -->

        <div class="column column-content column-double column-last">
            <div id="crumbs">
            <!--?php the_breadcrumb(); ?-->
<?php
$args = array(
    'prefix' => '<p>',
    'suffix' => '</p>',
    'title' => __( '', 'breadcrumbs-plus' ),
    'home' => __( 'Home', 'breadcrumbs-plus' ),
    'sep' => '»',
    'front_page' => false,
    'bold' => true,
    'show_blog' => false,
    'echo' => true,
    'singular_post_taxonomy' => 'category'
);

breadcrumbs_plus( $args );
?>
            </div>

<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h1><?php single_cat_title(); ?></h1>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h1>Archive for: <?php single_tag_title(); ?></h1>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h1>Archive for <?php the_time('F jS, Y'); ?></h1>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h1>Archive for <?php the_time('F, Y'); ?></h1>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h1>Archive for <?php the_time('Y'); ?></h1>
<?php /* If this is an author archive */ } ?>

<h1><?php single_post_title(); ?></h1>
<?php
wp_reset_query();

  $args = array(
			'category_name' => 'individual-projects',
			'meta-key' => 'details_project_category',
			'meta_value' => $post->ID
		);

$wp_query = new WP_Query( $args );
$wp_query->get_posts();

$current = array();
$past = array();
if ( have_posts() ) {
    while ( have_posts() ) {
	the_post();
	$end = get( 'project_detail_end_year' );
	$begin = get( 'project_detail_start_year' );
	$abstract = get( 'project_detail_abstract' );

	//present
        if( $end >= date('Y') ) {
            $current[] = $post;
			$begin_dates[] = $begin;
			$end_dates[] = $end;
        }
        else {
            $past[] = $post;
			$begin_dates[] = $begin;
        }
    } //end while haveposts
	
	if ( $begin_dates ) {
		sort( $begin_dates );
		$oldest = $begin_dates[0];
	}
	if ( $end_dates ) {
		rsort( $end_dates );
		$newest = $end_dates[0];
		//pass newest to archive_filter (as 4th parameter) if archive should start a yr other than the current yr
		//may be a future date if project is set to start or end in future, or it may be a past date if there are no current projects
		//otherwise, archive will start at current year
	}
?>
<?php	
	$yrs_back = '5';
	$oldest_shown = date('Y') - $yrs_back;
	//print filtering yrs
	archive_filter( $oldest, $yrs_back );
?>
	<div class="posts">

<?php

    if ( $current && is_array( $current ) ) {
?>
            <h3 id="current_projects" class='year-filter-heading'>Current Projects</h3>
            <h4 class='year-filter-none' style='display: hidden'></h4>	
<?php
    foreach ( $current as $order_post ) {
        $ID = $order_post->ID;
		$end_yr = get( 'project_detail_end_year', 1, 1, true, $ID );
		$current_ordered[$end_yr][] = $order_post;
    }

    krsort ( $current_ordered, SORT_NUMERIC );
        foreach ( $current_ordered as $current_posts ) {
            foreach ( $current_posts as $current_post ) {

           		$ID = $current_post->ID;
				$start_yr = get( 'project_detail_start_year', 1, 1, true, $ID );
				$end_yr = get( 'project_detail_end_year', 1, 1, true, $ID );
?>
            <div class='post <?php echo fill_numbers_between( $start_yr, $end_yr ); if ( in_array( $oldest_shown, fill_numbers_between( $start_yr, $end_yr, 'array' ) ) ) { echo "older"; } ?>'>
<?php
				unset($img);

		if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail() ) {
                    $attachedFile = wp_get_attachment_image_src( get_post_thumbnail_id($ID), '' );
                    $img = $attachedFile[0];
		}
		else {
                    if ($proud_cf_use == 'Yes') {
                        $img = get_post_meta($ID, $proud_cf_photo, true);
                    } // if CF used
                    else {
                        if (!$img) {
                            $img = catch_that_image($ID);
                        }
                    } // if CF not used
		}

		if ($img) {
?>
                <div class="thumb"><a href="<?php echo get_permalink( $ID ); ?>" rel="bookmark" title="<?php echo get_the_title( $ID ); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?h=80&amp;w=120&amp;zc=1&amp;src=<?php echo $img ?>" alt="<?php the_title(); ?>" class="bordered" /></a></div>
<?php
		}

		$faux_cat_post_id = get( 'details_project_category', 1, 1, true, $ID );
		$faux_cat_link = get_permalink( $faux_cat_post_id );
		$faux_cat_post = get_post( $faux_cat_post_id );
		$faux_cat = $faux_cat_post->post_title;
		$individual_project = $current_post->post_name;
		$faux_post_link = add_query_arg( 'project', $individual_project, $faux_cat_link );
		$start_yr = get( 'project_detail_start_year', 1, 1, true, $ID );
		$end_yr = get('project_detail_end_year', 1, 1, true, $ID);
		if ( $start_yr !== $end_yr ) { $years = $start_yr.'-'.$end_yr; } else { $years = $end_yr; }
?>
		<h2><a href="<?php echo $faux_post_link; ?>" rel="bookmark" title="<?php echo get_the_title( $ID ); ?>"><?php echo get_the_title( $ID ); ?></a></h2>

		<div class="project-info"><?php echo magic_excerpt( get( 'project_detail_abstract', 1, 1, true, $ID ) ); ?></div>
        <div class='project-tags'><?php echo "Filed under: <span><a title='View all projects under ".$faux_cat."' href='".$faux_cat_link."'>".$faux_cat."</a></span>"; ?> <span><?php echo $years; ?></span></div>
		<div class="clear">&nbsp;</div>

	</div><!--.post-->
<?php
		} //end foreach
	} //endforeach
    } //end if current & is_array current
	else { echo "
	<h3 id='current_projects' style='display: none' class='year-filter-heading hidden'></h3>
	<h3 id='no-projects' class='year-filter-none show'>No current projects</h3>"; }
        if ( $past && is_array( $past ) ) {
?>
        <h3 id="past_projects" class='year-filter-hide'>Past Projects</h3>
<?php
            foreach ( $past as $order_post ) {
                $ID = $order_post->ID;
                $end_yr = get( 'project_detail_end_year', 1, 1, true, $ID );
                $past_ordered[$end_yr][] = $order_post;
            }

            krsort ( $past_ordered, SORT_NUMERIC );
            foreach ( $past_ordered as $past_posts ) {
                foreach ( $past_posts as $past_post ) {

            $ID = $past_post->ID;
			$start_yr = get( 'project_detail_start_year', 1, 1, true, $ID );
			$end_yr = get( 'project_detail_end_year', 1, 1, true, $ID );
?>
            <div class='post <?php echo fill_numbers_between( $start_yr, $end_yr ); if ( in_array( $oldest_shown, fill_numbers_between( $start_yr, $end_yr, 'array' ) ) ) { echo "older"; } ?>'>
<?php
			
            unset($img);
                if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail() ) {
                    $attachedFile = wp_get_attachment_image_src( get_post_thumbnail_id($ID), '' );
                    $img = $attachedFile[0];
                } else {
                    if ($proud_cf_use == 'Yes') {
                        $img = get_post_meta($ID, $proud_cf_photo, true);
                    } // if CF used
                    else {
                        if (!$img) {
                            $img = catch_that_image($ID);
                        }
                    } // if CF not used
                }

                if ($img) {
?>
                <div class="thumb"><a href="<?php echo get_permalink( $ID ); ?>" rel="bookmark" title="<?php echo get_the_title( $ID ); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?h=80&amp;w=120&amp;zc=1&amp;src=<?php echo $img ?>" alt="<?php the_title(); ?>" class="bordered" /></a></div>
<?php
                }

                $faux_cat_post_id = get( 'details_project_category', 1, 1, true, $ID );
                $faux_cat_link = get_permalink( $faux_cat_post_id );
                $faux_cat_post = get_post( $faux_cat_post_id );
                $faux_cat = $faux_cat_post->post_title;
                $individual_project = $past_post->post_name;
                $faux_post_link = add_query_arg( 'project', $individual_project, $faux_cat_link );
				$start_yr = get( 'project_detail_start_year', 1, 1, true, $ID );
				$end_yr = get('project_detail_end_year', 1, 1, true, $ID);
				if ( $start_yr !== $end_yr ) { $years = $start_yr.'-'.$end_yr; } else { $years = $end_yr; }
?>
                <h2><a href="<?php echo $faux_post_link; ?>" rel="bookmark" title="<?php echo get_the_title( $ID ); ?>"><?php echo get_the_title( $ID ); ?></a></h2>

                <div class="project-info"><?php echo magic_excerpt( get( 'project_detail_abstract', 1, 1, true, $ID ) ); ?></div>
                <div class='project-tags'><?php echo "Filed under: <span><a title='View all projects under ".$faux_cat."' href='".$faux_cat_link."'>".$faux_cat."</a></span>"; ?> <span><?php echo $years; ?></span></div>
                <div class="clear">&nbsp;</div>
            </div><!--.post-->
<?php
                } //end foreach
            } //endforeach
        } //end if past
} //end if haveposts
else {
?>
            <h2>Sorry, there are no posts in this category.</h2>
<?php
}
?>
            <div class="navigation">
                <p><span class="older"><?php next_posts_link('&lt; older entries') ?></span>  <span class="latest"><?php previous_posts_link('newer entries &gt;') ?></span></p>
            </div>
	</div><!-- end .posts -->

    </div><!-- end .column-content -->

    <div class="clear">&nbsp;</div>

    </div><!-- end .wrap -->

</div><!-- end #content -->

<?php get_footer(); ?>
